# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
#
ARG IMAGE_NAME
ARG IMAGE_TAG
FROM ${IMAGE_NAME}:${IMAGE_TAG}
LABEL maintainer="Apache NiFi <dev@nifi.apache.org>"
LABEL site="https://nifi.apache.org"

ARG UID=1000
ARG GID=1000
ARG NIFI_TOOLKIT_VERSION=2.0.0
ARG NIFI_TOOLKIT_BINARY=nifi-toolkit-${NIFI_TOOLKIT_VERSION}-bin.zip

ENV NIFI_TOOLKIT_BASE_DIR=/opt/nifi-toolkit
ENV NIFI_TOOLKIT_HOME=${NIFI_TOOLKIT_BASE_DIR}/nifi-toolkit-current

RUN mkdir -p ${NIFI_TOOLKIT_BASE_DIR}
COPY $NIFI_TOOLKIT_BINARY $NIFI_TOOLKIT_BASE_DIR
ADD sh/ ${NIFI_TOOLKIT_BASE_DIR}/scripts

RUN chmod -R +x ${NIFI_TOOLKIT_BASE_DIR}/scripts/*.sh \
   && apt-get update \
   && apt-get install -y unzip jq openssl \
   && apt-get -y autoremove \
   && apt-get clean autoclean \
   && rm -rf /var/lib/apt/lists/* \
   && groupadd -g ${GID} nifi \
   && useradd --shell /bin/bash -u ${UID} -g ${GID} -m nifi \
   && unzip ${NIFI_TOOLKIT_BASE_DIR}/${NIFI_TOOLKIT_BINARY} -d ${NIFI_TOOLKIT_BASE_DIR} \
   && rm ${NIFI_TOOLKIT_BASE_DIR}/${NIFI_TOOLKIT_BINARY} \
   && ln -s ${NIFI_TOOLKIT_BASE_DIR}/nifi-toolkit-${NIFI_TOOLKIT_VERSION} ${NIFI_TOOLKIT_HOME} \
   && chown -R nifi:nifi ${NIFI_TOOLKIT_BASE_DIR}

USER nifi

WORKDIR ${NIFI_TOOLKIT_HOME}

# Startup NiFi
ENTRYPOINT ["../scripts/docker-entrypoint.sh"]
